package com.software.seakdream.service;

import com.software.seakdream.entity.AudioInfo;
import com.software.seakdream.entity.DailyAudio;
import com.software.seakdream.entity.History;
import com.software.seakdream.entity.HistoryRecord;
import com.software.seakdream.repository.AudioInfoRepository;
import com.software.seakdream.repository.DailyAudioRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * @description:
 * @author：zzh
 * @date: 2023/5/31
 * @Copyright：
 */
@Service
public class DailyAudioServiceImpl implements DailyAudioService{
    @Autowired
    private DailyAudioRepository dailyAudioRepository;
    @Autowired
    private AudioInfoRepository audioInfoRepository;
    @Override
    public void createDaily(DailyAudio dailyAudio) {
        dailyAudioRepository.createDaily(dailyAudio);
    }

    @Override
    public List<HistoryRecord> getDailyAudio(String userId, int page, int pageSize) {
        List<DailyAudio> dailyAudios = dailyAudioRepository.findInRange(userId,(page-1)*pageSize,pageSize);
        List<HistoryRecord> records = new ArrayList<HistoryRecord>();
        AudioInfo audioInfo;
        for(DailyAudio history:dailyAudios){
            HistoryRecord record = new HistoryRecord();
            record.setHistoryId(history.getDailyId());
            record.setWhiteNoise(history.getIsWhite() != null);
            if(record.isWhiteNoise()){
                int count = 0;
                if(history.getWhiteNoise1()!=null && !history.getWhiteNoise1().equals("")){
//                    System.out.println("whiteNoise1=" + history.getWhiteNoise1());
                    audioInfo = audioInfoRepository.findAudioInfoById(history.getWhiteNoise1());
                    record.setWhiteId1(audioInfo.getAudioId());
                    record.setWhiteNoiseImg1(audioInfo.getAudioPic());
                    record.setWhiteNoiseName1(audioInfo.getAudioName());
                    record.setWhiteNoiseUrl1(audioInfo.getAudioUrl());
                    count++;
                }
                if(history.getWhiteNoise2()!=null && !history.getWhiteNoise2().equals("")){
//                    System.out.println("whiteNoise2=" + history.getWhiteNoise2());
                    audioInfo = audioInfoRepository.findAudioInfoById(history.getWhiteNoise2());
                    record.setWhiteId2(audioInfo.getAudioId());
                    record.setWhiteNoiseImg2(audioInfo.getAudioPic());
                    record.setWhiteNoiseName2(audioInfo.getAudioName());
                    record.setWhiteNoiseUrl2(audioInfo.getAudioUrl());
                    count++;
                }
                if(history.getWhiteNoise3()!=null && !history.getWhiteNoise3().equals("")){
//                    System.out.println("whiteNoise2=" + history.getWhiteNoise2());
                    audioInfo = audioInfoRepository.findAudioInfoById(history.getWhiteNoise3());
                    record.setWhiteId3(audioInfo.getAudioId());
                    record.setWhiteNoiseImg3(audioInfo.getAudioPic());
                    record.setWhiteNoiseName3(audioInfo.getAudioName());
                    record.setWhiteNoiseUrl3(audioInfo.getAudioUrl());
                    count++;
                }
                record.setWhiteNoiseCount(count);
            }else{
                record.setAudioId(history.getAudioId());
                audioInfo = audioInfoRepository.findAudioInfoById(history.getAudioId());
                record.setAudioImg(audioInfo.getAudioPic());
                record.setAudioName(audioInfo.getAudioName());
                record.setAudioAuthor(audioInfo.getAudioAuthor());
                record.setAudioUrl(audioInfo.getAudioUrl());
                record.setAudioType(audioInfo.getType());
            }
            records.add(record);
        }
        return records;
    }

}
